package com.itheima.controller;

import com.itheima.bean.User;
import com.itheima.service.UserService;
import org.apache.commons.beanutils.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;

@Controller
@ResponseBody
@RequestMapping("user")
public class UserController{

    @Autowired
    private UserService userService;

    /**
     * 处理登录的请求
     * @throws ServletException
     * @throws IOException
     */
    @RequestMapping("/login")
    protected boolean login(User user, HttpSession session){
        User login = userService.login(user);
        //判断账号密码是否为null
        if (login != null) {
            //不为null则添加到会话域中
            session.setAttribute("user",login);//要将信息放进session里面, 因为过滤器要从会话域中拿数据进行判断
        }

        return login != null;
    }

}
